var DEBUG = true;

function debug(msg) {
	if(DEBUG) {
		$('#debug').append(msg+'<br />');
	}
}

function isEmpty(elem) {
	if(elem.value.length == 0) {
		return true;
	}
	return false;
}

function isEqual(elem1, elem2) {
	if(elem1.value == elem2.value) {
		return true;
	} else {
		return false;
	}
}

function isAlphabet(elem){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)) {
		return true;
	}else{
		return false;
	}
}

function isAlphanumeric(elem){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)) {
		return true;
	} else {
		return false;
	}
}

function lengthRestriction(elem, min, max) {
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max) {
		return true;
	} else {
		return false;
	}
}

function countChecked(elem) {
	var n = $('#action_reg #interests input:checked').length; // elem.length;
	if(n>=1) {
		return true;
	} else {
		return false;
	}
}

function emailValidator(elem) {
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)) {
		return true;
    } else {
    	return false;
	}
}

function showHint(selector, msg) {
	$(selector).parent().siblings('.hint_td').text(msg);
}

function formValidator(){
	// Make quick references to our fields
	var username = document.getElementById('usr');
	var password = document.getElementById('pwd');
	var confirmedpassword = document.getElementById('pwd_again');
	var firstname = document.getElementById('fname');
	var lastname = document.getElementById('lname');
	
	var category = document.getElementById('Category');

	
	var ret = 0;
	// Check each input in the order that it appears in the form!
	// USERNAME: 
	debug('just enter');
	if(isEmpty(username)) {
		showHint('#usr', 'Username could not be empty');
	} else if(!lengthRestriction(username, 5 ,256)) {
		showHint('#usr', 'Username should be a valid email');
	} else if(!emailValidator(username)) {
		showHint('#usr', 'Username should be a valid email');
	} else {
		ret++;
	}
	
	// PASSWORD
	if(isEmpty(password)) {
		showHint('#pwd', 'Password could not be empty');
	} else if(!lengthRestriction(password, 6, 20)) {
		showHint('#pwd', 'Length should be between 6 and 20');
	} else if(!isAlphanumeric(password)) {
		showHint('#pwd', 'Password should only consist of letters and numbers');
	} else if(!isEqual(password, confirmedpassword)) {
		showHint('#pwd_again', 'Password doesn\'t match');
	} else {
		ret++;
	}
	
	// FIRST NAME
	if(isEmpty(firstname)) {
		showHint('#fname', 'Firstname could not be empty');
	} else if(!isAlphabet(firstname)) {
		showHint('#fname', 'Firstname should only consist of letters');
	} else if(!lengthRestriction(firstname, 1, 48)) {
		showHint('#fname', 'Length should be less than 48');
	} else {
		ret++;
	}
	
	// LAST NAME
	if(isEmpty(lastname)) {
		showHint('#lname', 'Lastname could not be empty');
	} else if(!isAlphabet(lastname)) {
		showHint('#lname', 'Lastname should only consist of letters');
	} else if(!lengthRestriction(lastname, 1, 48)) {
		showHint('#lname', 'Length should be less than 48');
	} else {
		ret++;
	}
	
	// INTERESTS
	if(!countChecked(category)) {
		$('#action_reg #register_warning').text('At least 1 interest :)');
		$('#action_reg .hint').show();
	} else {
		ret++;
	}
	
	return (ret == 5);
}




$(document).ready(function() {
	$('#action_reg #response').hide();
	
	$('#action_reg #submit').click(function() {
		$('.hint_td').empty();
		$('#register_warning').empty();
		$('#register_warning').hide();
		
		if(formValidator()) {
			var strCategory = '';
			$('#action_reg #interests input:checkbox:checked').each(function (i){
				strCategory += $(this).val() + ',';
			});
			
			$('#response').load(
				'registerCheck', 		/// action!
				{ username : $('#usr').val(),
					password : $('#pwd').val(),
					lastname : $('#lname').val(),
					firstname : $('#fname').val(),
					category : strCategory},
				function () {
					var strData = $('#response').text();
					if(strData.length > 0) {
						debug(strData);
						var objData = jQuery.parseJSON(strData);
						if(objData.redirect != null) {
							
							window.location = objData.redirect;
							
						} else if(objData.warning != null) {
							showHint('#usr', strData);
						} else {
							// Do nothing
						}
					}
				}
			);
		} else {
			// invalid
		}
	});
});